package com.mdms.utils;

import java.util.MissingResourceException;
import java.util.ResourceBundle;

import javax.faces.application.Application;
import javax.faces.context.FacesContext;
import javax.faces.el.ValueBinding;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import javax.faces.application.FacesMessage;



public class FacesUtils {

	private static String defLocale;

	private static String serwerNo;

	private static String layout;
	
	public static final String NO_RESOURCE_FOUND = "Missing resource: ";


	public FacesUtils(){

	}


	public static void setManagedBeanValue(String beanName, Object newValue) {

		getManagedBeanValue(beanName);

		StringBuffer buff = new StringBuffer("#{");
		buff.append(beanName);
		buff.append("}");
		setExpressionValue(buff.toString(), newValue);


	}
	public static Object getManagedBeanValue(String beanName){
		return getManagedBeanValue(beanName, null);
	}

	public static Object getManagedBeanValue(String beanName,FacesContext context) {
		StringBuffer buff = new StringBuffer("#{");
		buff.append(beanName);
		buff.append("}");
		return resolveExpression(buff.toString(),context);

	}

	public static Object resolveExpression(String expression,FacesContext ctx) {
		if(ctx==null){
			ctx = getFacesContext();
		}
		Application app = ctx != null ? ctx.getApplication() : null;
		ValueBinding bind = app != null ? app.createValueBinding(expression) : null;
		return bind != null ? bind.getValue(ctx) : null;
	}

	public static FacesContext getFacesContext() {
		return FacesContext.getCurrentInstance();
	}

	public static void setExpressionValue(String expression, Object newValue) {
		FacesContext ctx = getFacesContext();
		Application app = ctx.getApplication();
		ValueBinding bind = app.createValueBinding(expression);
		Class bindClass = bind.getType(ctx);
		if (bindClass.isPrimitive() || bindClass.isInstance(newValue)) {
			bind.setValue(ctx, newValue);
		}
	}

	public static String getFromResources(String key) {

		ResourceBundle bundle = (ResourceBundle) getManagedBeanValue("resources");
		return getStringSafely(bundle, key, null);
	}
	
	private static String getStringSafely(ResourceBundle bundle, String key, String defaultValue) {
		String resource = null;
		try {
			resource = bundle.getString(key);
		} catch (MissingResourceException mrex) {
			if (defaultValue != null) {
				resource = defaultValue;
			} else {
				resource = NO_RESOURCE_FOUND + key;
			}
		} catch(NullPointerException ex){
			if(defaultValue != null){
				resource = defaultValue;
			} else{
				resource = NO_RESOURCE_FOUND + "null";
			}
		}
		
		return resource;
	}
	
	public static void showError(String sMessage) {
		showInform(sMessage, FacesMessage.SEVERITY_ERROR);
	}
	
	public static void showInform(String sMessage) {
		showInform(sMessage, FacesMessage.SEVERITY_INFO);
	}
	
	public static void showInform(String sMessage, FacesMessage.Severity severity) {
		FacesMessage facesMessage = new FacesMessage(sMessage, "");	
		facesMessage.setSeverity(severity);
		getFacesContext().addMessage(null, facesMessage);
	}	


	public static String getDefLocale() {
		return defLocale;
	}


	public static void setDefLocale(String defLocale) {
		FacesUtils.defLocale = defLocale;
	}


	public static String getSerwerNo() {
		return serwerNo;
	}


	public static void setSerwerNo(String serwerNo) {
		FacesUtils.serwerNo = serwerNo;
	}


	public static String getLayout() {
		return layout;
	}


	public static void setLayout(String layout) {
		FacesUtils.layout = layout;
	}






}
